﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Diagnostics;

namespace _1.Dating
{
    class DatingUtilities
    {
        static void Main()
        {
            ImportDatingInfoFromXmlToDB(@"C:\Temp\people.xml");            
        }

        public static void ImportDatingInfoFromXmlToDB(string path)
        {
            using (DatingEntities datingContext = new DatingEntities())
            {
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.IgnoreComments = true;
                settings.IgnoreWhitespace = true;
                XmlReader reader = XmlReader.Create(path,settings);
                
                bool isGuy = true;
                int cityID = 0; 
                while (reader.Read())
                {                    
                    if (reader.NodeType == XmlNodeType.Element && reader.Name == "guys")
                    {
                        isGuy = true;
                    }
                    if (reader.NodeType == XmlNodeType.Element && reader.Name == "girls")
                    {
                        isGuy = false;
                    }
                    if (reader.NodeType == XmlNodeType.Element && reader.Name == "city")
                    {
                        string cityName = reader["name"];
                        if (!datingContext.Cities.Any(c=> c.CityName==cityName))
                        {
                            City cityToAdd = new City() { CityName = cityName };
                            datingContext.Cities.AddObject(cityToAdd);
                            datingContext.SaveChanges();
                            cityID = cityToAdd.CityID;
                        }                        
                        reader.Read();                        
                    }
                    if (reader.NodeType == XmlNodeType.Element && reader.Name == "person")
                    {
                        string PersonName = reader["name"];
                        string HairColor = reader["haircolor"];
                        string eyecolor = reader["eyecolor"];
                        int height = int.Parse(reader["height"]);
                        int age = int.Parse(reader["age"]);
                        Person personToAdd = new Person()
                        {
                            Name = PersonName,
                            Age = age,
                            CityID = cityID,
                            EyeColor = eyecolor,
                            HairColor = HairColor,
                            Height = height,
                            Male = isGuy
                        };
                        datingContext.Persons.AddObject(personToAdd);
                        //datingContext.SaveChanges();
                        int personID = personToAdd.PersonID;
                        reader.Read();
                        if (reader.NodeType == XmlNodeType.Element && reader.Name == "preferences")
                        {
                            List<Preference> prefs = new List<Preference>();

                            while (true)
                            {
                                reader.Read();
                                if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "preferences")
                                {
                                    break;
                                }
                                bool isMale = true;
                                if (reader.Name == "guy")
                                {
                                    isMale = true;
                                }
                                else if (reader.Name == "girl")
                                {
                                    isMale = false;
                                }
                                reader.Read();
                                string[] prefHeight = reader.ReadElementContentAsString().Split('-');
                                int fromHeight = int.Parse(prefHeight[0]);
                                int toHeight = int.Parse(prefHeight[1]);
                                string prefHair = reader.ReadElementContentAsString();
                                string prefEyes = reader.ReadElementContentAsString();
                                string[] prefAge = reader.ReadElementContentAsString().Split('-');
                                int fromAge = int.Parse(prefAge[0]);
                                int toAge = int.Parse(prefAge[1]);
                                Preference prefToAdd = new Preference()
                                {
                                    PersonID = personID,
                                    Male = isMale,
                                    FromHeight = fromHeight,
                                    ToHeight = toHeight,
                                    FromAge = fromAge,
                                    ToAge = toAge,
                                    HairColor = prefHair,
                                    EyesColor = prefEyes
                                };

                                personToAdd.Preferences.Add(prefToAdd);

                                //prefs.Add(prefToAdd);
                                
                                //datingContext.Preferences.AddObject(prefToAdd);
                            }                                                        
                        }

                        datingContext.SaveChanges();
                    }
                }
            }
        }
    }
}
